<?php

namespace app\zhongcan\controller;

use api\web\model\NewsModel;
use app\zhongcan\validate\NewsValidate;
use cmf\controller\AdminBaseController;
use think\Request;

class NewsController extends AdminBaseController
{
    //页面
    public function index(Request $request)
    {
        $name = $request->get('name');
        $where = [];
        if ($name) {
            $where[] = ['title','like','%'.$name.'%'];
        }
        $res = NewsModel::order('id','desc')
            ->where($where)
            ->paginate(3);
        $this->assign('list',$res);
        return $this->fetch('news/index');
    }

    //添加内容
    public function add()
    {
        return $this->fetch('news/add');
    }
    //提交添加
    public function addPost(Request $request)
    {
        $data = $request->param();
        //图片
        $data['image'] = cmf_get_image_url($data['image']);
        //创建时间
        $create_time = date("Y-m-d H-i-s");
        //验证器
//        $validate = new NewsValidate();
//        $list = $validate->scene('add')->check($data);
//        if (!$list) {
//           $this->error($validate->getError());
//        }
        $res = NewsModel::create([
            'image'=>$data['image'],
            'abstract'=>$data['abstract'],
            'content'=>$data['content'],
            'author'=>$data['author'],
            'create_time'=>$create_time
        ]);
        if ($res) {
            $this->success('添加成功');
        }else{
            $this->error('添加失败');
        }
    }

    //修改内容
    public function edit(Request $request)
    {
        $id = $request->param('id');
        $res = NewsModel::where('id','=',$id)
            ->find();
        $this->assign('res',$res);
        return $this->fetch('news/edit');
    }

    //提交修改
    public function editPost(Request $request)
    {
        $data = $request->param();
        //图片
        $data['image'] = cmf_get_image_url($data['image']);
        $res = NewsModel::where('id','=',$data['id'])
            ->update([
                'image'=>$data['image'],
                'abstract'=>$data['abstract'],
                'content'=>$data['content'],
                'author'=>$data['author'],
                'create_time'=>date('Y-m-d H:i:s')
            ]);
        if ($res) {
            return $this->success('修改成功');
        }else{
            return $this->error('修改失败');
        }
    }

    //删除
    public function delete(Request $request)
    {
        $id = $request->param('id/d');
        $list = NewsModel::where('id',$id)
            ->delete();
        if ($list) {
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    //排序
    public function order()
    {
        parent::listOrders(new NewsModel());
        $this->success('排序成功');
    }

    //是否展示
    public function show(Request $request)
    {
        $id = $request->param('id/d');
        $list = NewsModel::where('id',$id)->find();

        if ($list['switch'] == 1) {
            $list['switch'] = 0;
        }else{
            $list['switch'] = 1;
        }
        $res = NewsModel::where('id','=',$id)
            ->update([
                'switch'=>$list['switch']
            ]);
        if ($res) {
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }

    //文章详情
    public function content()
    {
        return '文章详情';
    }
}